<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery-2.1.0.js"></script>
<script src="js/pintuer.js"></script>
<script src="js/ajaxfileupload.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>修改教师</strong></div>
  <div class="body-content">
    <form class="form-x" >  
      <div class="form-group">
        <div class="label">
          <label>教师名称：</label>
        </div>
        <div class="field">
          <input  id="teachername" type="text" class="input w50" value="" name="title" data-validate="required:请输入教师名称" />
          <div class="tips"></div>
        </div>
      </div>
      
      <div class="form-group">
     <label class="label">教师图片:</label>
           <div class="col-sm-8">
                <img id="image" class="cover-radius"  src="images/clickup2.jpg" style="width: 20%;height: 20%;"/>
                <input id="picture_upload" name="file" type="file" onchange="upload_cover(this)"
                           style="display: none;"/>
                <small class="help-block cover-tips" style="color: #dd4b39;display: none;">请上传照片</small>
           </div>
      </div>
      
        <div class="form-group">
          <div class="label">
            <label>所属年级：</label>
          </div>
          <div class="field">
            <select  id="gidselect"  name="cid" class="input w50">
                  <option value="">请选择</option>
              <option value="1">小学一年级</option>
            <option value="2">小学二年级</option>
              <option value="3">小学三年级</option>
                <option value="4">小学四年级</option>
                  <option value="5">小学五年级</option>
                    <option value="6">初中一年级</option>
                      <option value="7">初中二年级</option>
                        <option value="8">初中三年级</option>
                          <option value="9">初中四年级</option>
                            <option value="10">高中一年级</option>
                             <option value="11">高中二年级</option>
                              <option value="12">高中三年级</option>
            </select>
            <div class="tips"></div>
          </div>
        </div>
        
        <div class="form-group">
          <div class="label">
            <label>所属课程：</label>
          </div>
          <div class="field">
            <select id="smycourse"   class="input w50">
            </select>
            <div class="tips"></div>
          </div>
        </div>
        
        
         <div class="form-group">
          <div class="label">
            <label>教师级别：</label>
          </div>
          <div class="field">
            <select  id="levelselect"  name="cid" class="input w50">
                  <option value="">请选择</option>
              <option value="1">1</option>
            <option value="2">2</option>
              <option value="3">3</option>
            </select>
            <div class="tips"></div>
          </div>
        </div>
        
      <div class="form-group">
        <div class="label">
          <label>教师简介：</label>
        </div>
        <div class="field">
          <textarea   id="teacherintro" class="input" name="note" style=" height:90px;"></textarea>
          <div class="tips"></div>
        </div>
      </div>
       <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
        	 <input  class="button bg-main icon-check-square-o"  style="text-align: center;"  type="button" name="back" value="确定提交" onclick="modify()"/>
        </div>
      </div>
    
    </form>
    
    
  </div>
</div>

</body>

<script type="text/javascript">
	
	    var val;
	    var mygidchoice;
		  var mycidchoice;
		  var mylevel;
		  var pic;
		  var pictime;
		   $(window).load(function() {
               courseinit();
               viewInit();
			 });
			 
			 /*初始化教师数据*/
			 function viewInit()
			 {
			 	 val = {
			 	       "mode":"6",
			 	       "teacherid":window.location.href.split("=")[1]
			          };
			 	  var url = 'http://119.3.13.186:8080/ZDEduBooking/servlet/QueryTeacherServlet';
      $.ajax({
        url: url,
      	type: 'POST',
        data: {
						DATA: JSON.stringify(val)
			  },
			  async : false,  
        dataType: 'json',
        crossDomain: true,
        success: function(data) {
        	
        	  for(var i = 0; i < data.tlist.length; i++) {
        	  	/*界面初始化*/
        	  $('#teachername').attr("value",data.tlist[i].teachername);
        		$('#gidselect').find("option:selected").text(data.tlist[i].gradeName);
        		$('#smycourse').find("option:selected").text(data.tlist[i].coursename);
        		$('#levelselect').find("option:selected").text(data.tlist[i].teacherlevel);
        		$('#teacherintro').html(data.tlist[i].teacherintro);
        		
        		$("#image").attr("src", data.tlist[i].teacherpic) ;
        		mygidchoice=data.tlist[i].gradeid;
        		mycidchoice=data.tlist[i].courseid;
        		mylevel=data.tlist[i].teacherlevel;
        		pic=data.tlist[i].teacherpic;
        	  }
        	
        },
						error: function(xhr, type, errorThrown) {
							alert("服务器异常，请稍后再试:" + type);
						}
      });
			 }
	
	
	    /*获取课程名列表*/
		  function courseinit()
		  {
		  	 
		  	val = {
			 };
		  	 $("#smycourse").empty();
		  	 $("#smycourse").append('<option value="">请选择</option>');
		  	  var url =  'http://119.3.13.186:8080/ZDEduBooking/servlet/QueryCourseServlet';
      $.ajax({
        url: url,
      	type: 'POST',
        data: {
						DATA: JSON.stringify(val)
			  },
        dataType: 'json',
        crossDomain: true,
        success: function(data) {
        	if(data.clist!=null)
        	{
        		  for(var i = 0; i < data.clist.length; i++) {
							var cid = data.clist[i].courseid;
							var cname= data.clist[i].coursename;
							
             $("#smycourse").append("<option value='"+cid+"'>"+cname+"</option>");
						}
        	}
        
        },
						error: function(xhr, type, errorThrown) {
							alert("服务器异常，请稍后再试:" + type);
						}
      });
		  	
		  }
		  
	
	    
		   $("#gidselect").change(function() { 
                mygidchoice = $(this).children('option:selected').val();  
            });  
          $("#smycourse").change(function() { 
                mycidchoice = $(this).children('option:selected').val();  
            });
             $('#levelselect').change(function() { 
                mylevel = $(this).children('option:selected').val();  
            });  
            
	
	    function  modify(){
	    	 if(pictime==null)
	    	 {
	    	 	val = {
		 	    "teacherid": window.location.href.split("=")[1],
		 	    "teachername": $('#teachername').val(),
				"teacherpic": pic,
				"teacherintro":$('#teacherintro').val(),
				"gradeid":mygidchoice,
				"courseid":mycidchoice,
				"teacherlevel":mylevel
			  };
	    	 }
	    	 else
	    	 {
	    	 	val = {
		 	    "teacherid": window.location.href.split("=")[1],
		 	    "teachername": $('#teachername').val(),
				"teacherpic": 'http://119.3.13.186:8080/ZDEduBooking/uploadfiles/'+pictime+'.jpg',
				"teacherintro":$('#teacherintro').val(),
				"gradeid":mygidchoice,
				"courseid":mycidchoice,
				"teacherlevel":mylevel
			  };
	    	 }
	    	
   		 
		 	
      var url = 'http://119.3.13.186:8080/ZDEduBooking/servlet/UpdateTeacherServlet';
      $.ajax( {
      	url: url,
      	type: 'POST',
        data: {
						DATA: JSON.stringify(val)
			  },
        dataType: 'json',
        crossDomain: true,
        success: function(data) {
                if(data.result=='0')
               {
               	   alert("修改教师成功");
                     history.back(-1);
               }
               else{
               	alert("修改教师失败");
               }
        },
						error: function(xhr, type, errorThrown) {
							   alert("服务器异常，请稍后再试:" + type);
						}
					});
					
   	};
   	
   	$(function() {
	
$("#image").click(function () {
$("#picture_upload").click(); //隐藏了input:file样式后，点击头像就可以本地上传
$("#picture_upload").on("change",function(){
var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径，该路径不是图片在本地的路径
if (objUrl) {
$("#image").attr("src", objUrl) ; //将图片路径存入src中，显示出图片
}
});
});
});
 
//建立一個可存取到該file的url
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
   	
   	
   	　function upload_cover(obj) {
        ajax_upload(obj.id, function(data) { //function(data)是上传图片的成功后的回调方法
//          var isrc = data.picaddr.replace(/\/\//g, '/'); //获取的图片的绝对路径
//          $('#image').attr('src', basePath+isrc).data('img-src', isrc); //给<input>的src赋值去显示图片
        });
    }
    function ajax_upload(feid, callback) { //具体的上传图片方法
    		 pictime=getNowFormatDate();
    		console.log(pictime)
        if (image_check(feid)) { //自己添加的文件后缀名的验证
            $.ajaxFileUpload({
                fileElementId: feid,    //需要上传的文件域的ID，即<input type="file">的ID。
//              url: "http://192.168.1.187:8080/ZDEduBooking/servlet/SavePicServlet", //后台方法的路径
                url: "http://119.3.13.186:8080/ZDEduBooking/servlet/SavePicServlet", //后台方法的路径
                type: 'POST',   //当要提交自定义参数时，这个参数要设置成post
                dataType: 'JSON',   //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
                secureuri: false,   //是否启用安全提交，默认为false。
                async : false,   //是否是异步
                data: {"pictitle":pictime} ,
                success: function(data) {   //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
                    if (callback) callback.call(this, data);
                  },
                error: function(data, status, e) {  //提交失败自动执行的处理函数。
                    console.error("错误的结果="+e+data);
                }
            });
        }
    }
    function image_check(feid) { //自己添加的文件后缀名的验证
        var img = document.getElementById(feid);
        return /.(jpg|png|gif|bmp)$/.test(img.value)?true:(function() {
            modals.info('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
            return false;
        })();
    }
    
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        var hours = date.getHours();
        var minus=date.getMinutes();
        var sec=date.getSeconds();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + month  + strDate+hours+minus+sec;
        return currentdate;
    }
   	
</script>


</html>